
/**
 * @param <T> Generic type.
 * @author Lukas Koell
 * @author David Medenjak
 * @author Johannes Riedmann
 */
public class InorderTree<T extends Comparable<T>> extends SortedTree<T> {

    @Override
    public TreeIterator<T> iterator() {
        return new InorderIterator<T>(this);
    }

    /**
     * This Iterator iterates over the SortedTree in an Inorder-way
     * @author Lukas Koell
     * @author David Medenjak
     * @author Johannes Riedmann
     */
    private class InorderIterator<Ti extends Comparable<? super Ti>> extends TreeIterator<Ti> {

        public InorderIterator(Tree<Ti> tree) {
            super(tree);
        }

        /**
         * Determines how to set up the linked list.
         */
        @Override
        protected void flattenImplementation() {
            inorder(tree.getRoot());
        }

        /**
         * Inorder traversing.
         * @param node
         */
        private void inorder(Node<Ti> node) {
            if (node == null) {
                return;
            }
            inorder(node.getLeftChild());
            setNextItem(node);
            inorder(node.getRightChild());
        }
    }
}
